<?php
/**
 * 获得重整医嘱sql
 * @authors xiadx (xiadx@zpdchina.com)
 * @date    2017-03-10 10:06:39
 * @version $Id$
 */

class GettingzhiyizhusqlController extends Yaf_Controller_Abstract {

    public function indexAction()
    {
    	$request = $this->getRequest();
    	$user_department = $request->getPost('user_department', '');
    	if (empty($user_department)) {
    		Utility::apiJsonError(10000, '科室不能为空!');
    	}
    	$user_department_id = $request->getPost('user_department_id', '');
    	$data = $request->getPost('data', '');
    	$modify_time = $request->getPost('modify_time', '');
    	$compression_http_data = $request->getPost('compression_http_data', 'off');
    	$json_data = Utility::uncompress($data, $compression_http_data);

        // add by xiadx @20170316
        $yiyuan_id = $request->getPost('yiyuan_id', 0);
    	$time = intval (date("Hi"));
    	$async_result['error'] = '200';
    	$async_result['msg'] = '';
    	// 删除长期不显示
    	if ($time <= Yaconf::get("config$yiyuan_id.show_new_yizhu") || $time >= '2358') {
    		$async_result["update_sql"][] = "delete from yizhu_info where yizhu_type='长期'";
    	}

    	// 删除长期重整医嘱
    	$query = DB::select(DB::expr("CONCAT('100',a.id) as id,CONCAT('100',a.id) as ids"))
    				->from('zhuyuan_basic_info as t')
    				->join('zhuyuan_yizhu_changqi as a', 'left')
    				->on('t.zhuyuan_id', '=', 'a.zhuyuan_id');
		$query->where('t.zhuyuan_bingqu', '=', $user_department)
			  ->and_where('a.state', '=', '重整医嘱');
	    if (empty($modify_time)) {
	    	$query->and_where('a.modify_time', '>=', DB::expr('DATE_SUB(now(), INTERVAL 24 HOUR)'));
	    } else {
	    	$query->and_where('a.modify_time', '>=', $modify_time);
	    }
	    $chongzheng = $query->execute()->as_array();
	    $ids = Utility::array_pluck($chongzheng, 'ids');

	    if (empty($ids)) {
	    	$async_result['update_sql'][] = "delete from yizhu_info where yizhu_id in(".implode(',', $ids).")";
	    }
	    Utility::apiJsonResultExtends($async_result);
    }
}
